package com.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import com.wink.charity.Charity;
import com.wink.charity.CharityList;
import com.wink.common.Identity;
import com.wink.dt.*;
import com.wink.event.*;
import com.wink.subscribe.Subscription;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;


public class ReachOutClient {
	private String endPoint = "http://localhost:8080/ReachOut/rest/community";

	public ReachOutClient() {
	}

	public ReachOutClient(String url) {
		endPoint = url;
	}
	
	public void demoServices() {
		System.out.println("\n **List demoServices: ");
		try {
			String url = endPoint + "/services";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			// we can get the return as a string or converted to an object
			// String response =
			// resource.accept(MediaType.APPLICATION_XML).get(String.class);
			@SuppressWarnings("unchecked")
			//Event e = resource.accept(MediaType.APPLICATION_XML).get(Event.class);
			String services = resource.accept(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Services :" + services);			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demolistCharities() {		
		System.out.println("\n **Client --> In listCharities");
		try {
			String url = endPoint + "/charity";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			String list = resource.accept(MediaType.APPLICATION_XML).get(String.class);
			System.out.println("ListCharity :Json ::" +  list);
			/*System.out.println("Charities:");
			for (Charity s : list.getList()) {
				System.out.println("Charity : " + s.getName() + 
						" Description " +  s.getDescription() +
						" Identity " +  s.getIdentity().toString());	
				System.out.println("Charity :Json ::" +  s.toJson(s));
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateCharity() {		
		System.out.println("\n **List demoCreateCharity: ");
		try {
			String url = endPoint + "/charity";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			
			Charity c = new Charity();
			c.setName("Charity5");
			c.setDescription("Test creating a Charity, Charity 11 from a client");
				Identity identity = new Identity();
				identity.setFromCommunity("ABPMClient");
				identity.setFromUser("PKRClient");
				identity.setDate(new Date());	
			c.setIdentity(identity);

			@SuppressWarnings("unchecked")
			//Charity created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(Charity.class, c);
			String created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, Charity.toJson(c));
					
			System.out.println("Created Charity : " + created);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demoGetCharityByName() {
		System.out.println("\n** demoGetCharityByName:");
		try {
			String charityName = "Charity1";
			String url = endPoint + "/charity/" + charityName  ;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.accept(MediaType.APPLICATION_XML).get(String.class);

			System.out.println("Client --> demoGetCharityByName : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void donateCharity() {		
		System.out.println("\n **List donateCharity: ");
		try {
			String url = endPoint + "/charity/donate";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);	
			
			/*Charity c = new Charity();
			c.setName(" Charity from Client 11");
			c.setAmount("20");*/
			
			Charity c = new Charity();
			c.setName(" Charity from Client 11");
			c.setDescription("Test creating a Charity, Charity 11 from a client");
				Identity identity = new Identity();
				identity.setFromCommunity("ABPMClient");
				identity.setFromUser("PKRClient");
				identity.setDate(new Date());	
			c.setIdentity(identity);
			c.setAmount("20");

			@SuppressWarnings("unchecked")
			String donated = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(String.class, Charity.toJson(c));
					
			System.out.println("Donated to Charity : " + donated);				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demolistDiscussion() {		
		System.out.println("\n **Client --> In demolistDiscussion");
		try {
			String url = endPoint + "/discussionboard";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			String list = resource.accept(MediaType.APPLICATION_XML).get(String.class);
			System.out.println("ListTopics :Json ::" +  list);
			/*System.out.println("Charities:");
			for (Charity s : list.getList()) {
				System.out.println("Charity : " + s.getName() + 
						" Description " +  s.getDescription() +
						" Identity " +  s.getIdentity().toString());	
				System.out.println("Charity :Json ::" +  s.toJson(s));
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateTopic() {		
		System.out.println("\n **List demoCreateTopic: ");
		try {
			String url = endPoint + "/discussionboard";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			
	
			Topic t = new Topic();
			t.setTopicTitle("IPL");		
			t.setTopicdescription("Petrol prices increase");
				Identity identity = new Identity();
				identity.setFromCommunity("from Community ABPM");
				identity.setFromUser("from User PRK");
				identity.setDate(new Date());
				t.setIdentity(identity);
			
				// create Comment1 under Topic News
			Comment c = new Comment();
				c.setComment("News Comment 1");	
				c.setTopicName("News Topic");
				Identity identity2 = new Identity();
					identity2.setFromCommunity("from Community ABPM");
					identity2.setFromUser("from User PRK");
					identity2.setDate(new Date());
				c.setIdentity(identity2);
				t.addToCommentList(c);
				
				c = new Comment();
				c.setComment("News Comment 2");
				c.setTopicName("News Topic");	
					identity2 = new Identity();
					identity2.setFromCommunity("from Community ABPM");
					identity2.setFromUser("from User AVP");
					identity2.setDate(new Date());
				c.setIdentity(identity2);
				t.addToCommentList(c);

			@SuppressWarnings("unchecked")
			String created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, Topic.toJson(t));
					
			System.out.println("Created Topic : " + created);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateComment() {		
		System.out.println("\n **List demoCreateComment: ");
		try {
			String url = endPoint + "/discussionboard/comment";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			
			
			Comment c = new Comment();
				c.setComment("News Comment 1");	
				c.setTopicName("News Topic");
				Identity identity2 = new Identity();
					identity2.setFromCommunity("from Community ABPM");
					identity2.setFromUser("from User PRK");
					identity2.setDate(new Date());
				c.setIdentity(identity2);				

			@SuppressWarnings("unchecked")
			String created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, Comment.toJson(c));
					
			System.out.println("Created Comment : " + created);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoGetTopicByName() {
		System.out.println("\n** demoGetTopicByName:");
		try {
			// change according to data in mongoDB
			String topicName = "IPL";
			String url = endPoint + "/discussionboard/1/" + topicName  ;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.accept(MediaType.APPLICATION_XML).get(String.class);

			System.out.println("Client --> demoGetTopicByName : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoGetTopicDetails() {
		System.out.println("\n** demoGetTopicDetails:");
		try {
			// change according to data in mongoDB
			String topicName = "IPL";
			String url = endPoint + "/discussionboard/2/" + topicName  ;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			

			String response = resource.accept(MediaType.APPLICATION_XML).get(String.class);

			System.out.println("Client --> demoGetTopicDetails : " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoSubscribe() {
		System.out.println("\n** subcribeDemo:");
		try {
			String url = endPoint + "/subscribe/charity";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);			
			
			Subscription sub = new Subscription();
			sub.setEmail("Shilka7@gmail.com");
			sub.setName("Charity1");
//			sub.setAmount("500");
//			sub.setCommunity("Planet Granite");
//			sub.setCommunityID("SV");
			
			resource.contentType(MediaType.APPLICATION_JSON);
			resource.post(sub);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public static void main(String[] args) {
		ReachOutClient bc = new ReachOutClient("http://localhost:8080/ReachOut/rest/community");

				
		//Charity
		bc.demoCreateCharity();
		bc.demolistCharities();
		bc.demoCreateCharity();
		bc.demoGetCharityByName();
		bc.donateCharity();	
		
		//Discussion thread
//	   bc.demolistDiscussion();
//	   bc.demoCreateTopic();
//	   bc.demoCreateComment(); 
		//create topic named "IPL" to test
//	   bc.demoGetTopicByName();
		//create topic named "IPL" to test
//	   bc.demoGetTopicDetails();
		
	    //Subscription
//		bc.demoSubscribe();
//		bc.demoServices();
	}
}
